package com.hqyj.shop.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目:shop-admin-240701
 * 描述:
 * 时间:2024/10/17 11:17
 * 作者:admin
 * 版本:1.0
 **/
//@ServerEndpoint("/alipaySocket")
@Component
@Slf4j
public class AlipayWebsocket {

    //创建会话集合
    private static List<Session> list = new ArrayList<>();

    @OnOpen
    public void myOpen(Session session){
        log.info("{}用户进来了",session.getId());
        list.add(session);
    }

    //发送消息的函数
    public static void  sendMsg(String msg){
        //服务端发送消息到客户端
        try {
            for(Session s : list){
                //判断客户端是否打开
                if(s.isOpen()){
                    s.getBasicRemote().sendText(msg);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
